#!usr/bin/env python
# -*- coding:utf-8 _*-
"""
@author:zhuyan
@file: ndarray_demo.py
@time: 2018/10/06
"""
# 导入numpy
import numpy as np


class NdarraDemo:

    def  create(self):
        # 创建一维数组
        list1 = [5, 71, 25]
        narray = np.array(list1)

        # 创建二维数组
        list2 = [[5, 71, 25], [5, 71, 25, 99]]
        narray = np.array(list2)
        print(narray)

        a = np.arange(1,15,3)
        print(a)

        one = np.ones(10)
        print(one)

        em = np.empty((2, 3, 2))
        print(em)

        # 创建一个N*M的矩阵，对角线全1，其余为0
        e = np.eye(5, 5)
        print(e)

        #类型转换
        print(e.dtype)
        e.astype(np.int32)
        print(e.dtype)

        # 矢量运算
        print(e*e)


        # 标量运算
        print(2 * e)

    def index__(self):
        # np数组
        arr = np.arange(15)
        arr[5:8] = 8
        print(arr)

        s = arr[5:8].copy()
        print(s)
        print(arr)

        # python数组
        a = range(15)
        b = a[5:8]
        print(b)


    def index3d__(self):
        arr = np.eye(5, 5)
        print(arr[4][4])
        # 赋值标量给某一维的数组，会广播
        arr[3] = 9
        print(arr)
        # 赋值数组，维度与类型需要和原有相等
        arr[3] = [2,3,4,5,6]
        print(arr)
        # 此处会出错，因类型不同
        arr[3] = ["我","我","我","我","我"]
        print(arr)

    def slice__(self):
        arr = np.eye(5, 5)
        print(arr[:2])
        print(arr[:2,4:])
        print(arr[2, 4:])

    def bool__(self):
        names = np.asarray(["book", "cd", "dvd", "book", "book"])
        print(names=='book')
        datas = np.eye(5, 4)
        print(datas[[True, False, True, False, False]])

    def t__(self):
        arr = np.arange(16).reshape(2, 2, 4)
        print(arr)
        print(arr.T)

if __name__ == '__main__':
    demo = NdarraDemo()
    # demo.create()
    # demo.index__()
    # demo.index3d__()
    # demo.slice__()
    # demo.bool__()
    demo.t__()

